<template>
  <div class="app-container">
    <el-form :model="parkings" ref="p" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="门牌号" prop="houseNum">
        <el-input v-model="parkings.houseNum" placeholder="请输入门牌号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="车位编号" prop="code">
        <el-input v-model="parkings.code" placeholder="请输入车位编号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属停车场编码" prop="parkingLotCode">
        <el-select v-model="parkings.parkingLotCode" placeholder="请选择所属停车场编码" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.csd_parking_lot_code" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属停车场" prop="parkingLotName">
        <el-input v-model="parkings.parkingLotName" placeholder="请输入所属停车场" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属方式" prop="owerType">
        <el-select v-model="parkings.owerType" placeholder="请选择所属方式" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.csd_ower_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['park:park:add']">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parks">
      <el-table-column label="门牌号" align="center" prop="houseNum" :show-overflow-tooltip="true" />
      <el-table-column label="所属停车场编号" align="center" prop="parkingLotCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.csd_parking_lot_code" :value="scope.row.parkingLotCode" />
        </template>
      </el-table-column>
      <el-table-column label="所属停车场" align="center" prop="parkingLotName" />
      <el-table-column label="车位编号" align="center" prop="code" :show-overflow-tooltip="true" />
      <el-table-column label="所属方式" align="center" prop="owerType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.csd_ower_type" :value="scope.row.owerType" />
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="price" />
      <el-table-column label="到期时间" align="center" prop="endTime" />
      <el-table-column label="创建者" align="center" prop="createBy" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['park:park:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['park:park:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="parkings.pageNum" :limit.sync="parkings.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门牌号" prop="houseNum">
          <el-input v-model="form.houseNum" placeholder="请输入门牌号" />
        </el-form-item>
        <el-form-item label="所属停车场编号" prop="parkingLotCode">
          <el-select v-model="form.parkingLotCode" placeholder="请选择所属停车场编号">
            <el-option v-for="dict in dict.type.csd_parking_lot_code" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属停车场" prop="parkingLotName">
          <el-input v-model="form.parkingLotName" />
        </el-form-item>
        <el-form-item label="车位编号" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="所属方式" prop="owerType">
          <el-select v-model="form.owerType" placeholder="请选择所属方式" @change="blchange">
            <el-option v-for="dict in dict.type.csd_ower_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="到期时间" v-if="isok" prop="endTime">
          <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择日期时间" align="right"
                        value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions">
                    </el-date-picker>        </el-form-item>
        <el-form-item label="金额" prop="price">
          <el-input v-model="form.price" placeholder="自动赋值" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { selectPark, delPark, type, addPark, upPark } from "@/api/park/park";
export default {
  name: "park",
  dicts: ['csd_parking_lot_code', 'csd_ower_type'],
  data() {
    
   const sdgh= (rule, value, callback) => {  
        const today = new Date().getTime();  
        const inputDate = new Date(value).getTime();  
        if (inputDate < today) {  
          callback(new Error("到期时间必须大于当前时间！！！"));  
        } else {  
          callback();  
        }  
      }; 
    return {
      isok: false,
      //显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      //总条数
      total: 0,
      value1:"",
      // 选中数组
      //查询参数
      parkings: {
        pageNum: 1,
        pageSize: 10,
        houseNum: undefined,
        parkingLotName: undefined,
        parkingLotCode: undefined,
        code: undefined,
        owerType: undefined,
      },
      parks: [],
      ids: [],
      title: "",
      dialogVisible: false,
      form: {
        id: undefined,
        houseNum: undefined,
        parkingLotCode: undefined,
        parkingLotName: undefined,
        code: undefined,
        owerType: undefined,
        price: undefined,
        endTime: undefined,
      },
      rules: {

        houseNum: [
          { required: true, message: "门牌号不能为空", trigger: "blur" }
        ],
        parkingLotCode: [
          { required: true, message: "所属停车场编码不能为空", trigger: "blur" }
        ],
        parkingLotName: [
          { required: true, message: "所属停车场不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "车位编号不能为空", trigger: "blur" }
        ],
        owerType: [
          { required: true, message: "所属方式不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "到期时间不能为空", trigger: "blur" },
          { required: true, validator: sdgh, trigger: "blur" }

      
      
        ]
      },
      pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
    };

  },
  watch: {
    'form.endTime'(newVal) {
      let createTime;
      if (this.form.id !== undefined) {
        createTime = new Date(this.form.createTime);
      } else {
        createTime = new Date();
      }

// 按分钟计算，不足一分钟不计算钱数floor向下取整abs绝对值
      let date2 = new Date(newVal);
      let diffInDays = Math.abs(Math.floor((date2 - createTime) / (60 * 1000)));
      
      if (date2<createTime) {  
      this.form.price = 0; 
    }  else{
      if (this.form.owerType === "1") {
        this.form.price = 0;
      } else {
        if (isNaN(diffInDays)) {
          this.form.price = 0;
        } else {
          this.form.price = diffInDays* 0.01;
        }
      }
    }
      

    }
  },
  created() {
    this.getList();
  },
  methods: {

    blchange(selectValue) {
      if (selectValue == '0') {
        this.isok = true;
      }
      else {
        this.isok = false;
        this.form.price = 0;
      }
    },
    //搜索按钮操作
    handleQuery() {
      this.parkings.pageNum = 1;
      this.getList();
    },
    //显示列表
    getList() {
      selectPark(this.parkings).then(response => {
        this.parks = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //重置按钮操作
    resetQuery() {
      this.parkings = {
        pageNum: 1,
        pageSize: 10,
        houseNum: undefined,
        parkingLotName: undefined,
        parkingLotCode: undefined,
        code: undefined,
        owerType: undefined,
        price: undefined,
        endTime: undefined,
      },
        this.resetForm("p");
      this.handleQuery();
    },
    //删除
    handleDelete(row) {
      console.log(row)
      const id = row.id;
      this.$modal.confirm('是否确认删除为"' + id + '"的数据项？').then(function () {
        return delPark(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    //新增按钮操作
    handleAdd() {
      this.reset();
      this.dialogVisible = true;
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            upPark(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.dialogVisible = false;
              this.getList();
            });
          } else {
            addPark(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.dialogVisible = false;
              this.getList();
            });
          }
        }
      });
    },
    reset() {
      this.form = {
        id: undefined,
        houseNum: undefined,
        parkingLotCode: undefined,
        parkingLotName: undefined,
        code: undefined,
        owerType: undefined,
        price: undefined,
        endTime: undefined,
      };
      this.resetForm("form");
    },
    //修改 
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      type(id).then(response => {
        this.form = response.park;
        this.dialogVisible = true;
        this.title = "修改";
      });
    },

  }
};
</script>
<style scoped></style>